package cn.kooki.app.duobao.ui.Activity.User;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.User.LoginResponse;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.data.bus.OnRequestEvent;
import cn.kooki.app.duobao.ui.Adapter.TabAdapter;
import cn.kooki.app.duobao.ui.Fragment.duobaorecord.DuobaoRecordItemFragment;
import cn.kooki.app.duobao.ui.Fragment.share.UserCenterShareFragment;
import cn.kooki.app.duobao.ui.Fragment.winrecord.WinRecordFragment;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/17.
 */
public class UserCenterActivity extends BaseNavActivity {
    @Bind(R.id.user_avatar)
    ImageView userAvatar;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.user_id)
    TextView userId;
    @Bind(R.id.titti)
    RelativeLayout titti;
    @Bind(R.id.tabs)
    TabLayout tabs;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    private TabAdapter adapter;
    private String uid;
    private DuobaoRecordItemFragment duobaoRecordItemFragment;
    private UserInfo data;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.activity_user_center);
        ButterKnife.bind(this);

    }

    @Override
    protected void afterView() {
        uid = getIntent().getStringExtra("uid");
        setTitle(R.string.title_activity_user_center);
        adapter = new TabAdapter(getSupportFragmentManager());
        duobaoRecordItemFragment = new DuobaoRecordItemFragment();
        Bundle duobaob = new Bundle();
        duobaob.putInt("type", DuobaoRecordItemFragment.ALL_TYPE);
        duobaob.putString("uid", uid);
        duobaoRecordItemFragment.setArguments(duobaob);
        WinRecordFragment winRecordFragment = new WinRecordFragment();
        winRecordFragment.setArguments(duobaob);
        UserCenterShareFragment shareFragment = new UserCenterShareFragment();
        shareFragment.setArguments(duobaob);
        adapter.addFragment(duobaoRecordItemFragment, "抢宝记录");
        adapter.addFragment(winRecordFragment, "中奖纪录");
        adapter.addFragment(shareFragment, "晒单分享");
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        tabs.setupWithViewPager(viewPager);
        getuserdata();
    }

    private void getuserdata() {
        MainFactory.getApiInstance().getuserinfo(uid, new Callback<LoginResponse>() {


            @Override
            public void success(LoginResponse loginResponse, Response response) {
                if (loginResponse.getStatus() == 0) {
                    data = loginResponse.getData();
                    refreshView();
                } else {
                    showToastMessage(loginResponse.getMsg());
                }

            }


            @Override
            public void failure(RetrofitError error) {
                showToastMessage(getResources().getString(R.string.network_error));
            }
        });
    }


    public void onEventMainThread(ChangePageEvent event) {
        finish();
    }

    private void refreshView() {
        if (userName != null) {
            userName.setText(data.getUsername());
            String idstring = getString(R.string.user_id);
            idstring = String.format(idstring, data.getUid());
            userId.setText(idstring);
            Glide.with(this).load(data.getImg()).into(userAvatar);
        }
    }

}
